package com.icss.ah.fullReturn.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.fullReturn.service.FullReturnService;

@Controller
public class FullReturnAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private final Logger errlog = Logger.getLogger("E");
	
	@Autowired
	private FullReturnService fullReturnService;
	
	private String rtnId; //满返活动id
	private String cpsId; //代金券id
	private String rtnName;
	private String begDate;
	private String endDate;
	private String useCtn;//使用条件
	private String cpsPrice; //满返金额
	private String useDate;//生效时间
	private String status;
	
	public String toFullReturnListPage(){
		return "toFullReturnListPage";
	}
	
	public void queryFullReturnList(){
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("rtnName", rtnName);
			map.put("status", status);
			map.put("useDate", useDate);
			map.put("startIndex", getStartIndexMysql());
			map.put("endIndex", getEndIndexMysql());
			List<Map<String, Object>> list = fullReturnService.getFullReturnList(map);
			Long total = fullReturnService.getFullReturnListCount(map);
			dataGridJson = new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReturnAction|异常：",e);
		} finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
	}
	
	public String fullReturnPage(){
		if(null!=rtnId&&!"".equals(rtnId)){
			Map<String, Object> info = fullReturnService.getFullReturnInfo(rtnId);
			System.out.println("==========="+info);
			int cp = ((BigDecimal) info.get("cpsPrice")).intValue();
			info.put("cpsPrice", cp);
			int uc = ((BigDecimal) info.get("useCtn")).intValue();
			info.put("useCtn", uc);
			request = getRequest();
			request.setAttribute("info", info);
			request.setAttribute("flag", request.getParameter("flag"));
		}
		return "fullReturnPage";
	}
	
	public void save(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("rtnId", UUID.randomUUID().toString());
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("rtnName", rtnName);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("cpsPrice", cpsPrice);
			map.put("useCtn", useCtn);
			map.put("status", status);
			map.put("cpsId", cpsId);
			fullReturnService.saveInfo(map);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReturnAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void updateInfo(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("rtnId", rtnId);
			map.put("rtnName", rtnName);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("cpsPrice", cpsPrice);
			map.put("useCtn", useCtn);
			map.put("status", status);
			map.put("cpsId", cpsId);
			fullReturnService.updateInfo(map);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReturnAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void updateStatus(){
		JSONObject json = new JSONObject();
		try {
			fullReturnService.updateStatus(rtnId);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReturnAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void getCouponsList(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("begDate", "");
			map.put("endDate", "");
			String cpsName = getRequest().getParameter("cpsName");
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("cpsName", cpsName);
			List<Map<String, Object>> list = fullReturnService.getCouponsList(map);
			json.put("list", list);
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReturnAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}

	public void delInfo(){
		String result = fullReturnService.delInfo(rtnId);
		str2Resp(result);
	}

	public String getRtnId() {
		return rtnId;
	}

	public void setRtnId(String rtnId) {
		this.rtnId = rtnId;
	}

	public String getRtnName() {
		return rtnName;
	}

	public void setRtnName(String rtnName) {
		this.rtnName = rtnName;
	}

	public String getBegDate() {
		return begDate;
	}

	public void setBegDate(String begDate) {
		this.begDate = begDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getUseCtn() {
		return useCtn;
	}

	public void setUseCtn(String useCtn) {
		this.useCtn = useCtn;
	}

	public String getCpsPrice() {
		return cpsPrice;
	}

	public void setCpsPrice(String cpsPrice) {
		this.cpsPrice = cpsPrice;
	}

	public String getUseDate() {
		return useDate;
	}

	public void setUseDate(String useDate) {
		this.useDate = useDate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getCpsId() {
		return cpsId;
	}

	public void setCpsId(String cpsId) {
		this.cpsId = cpsId;
	}

}
